package com.answer.wx.common.response;

import lombok.Data;

import java.util.HashMap;
import java.util.List;

@Data
public final class ApiResponse {

    private int code;
    private String errorMsg;
    private Object data;
    private Boolean isLastPage;

    private ApiResponse(int code, String errorMsg, Object data, Boolean isLastPage) {
        this.code = code;
        this.errorMsg = errorMsg;
        this.data = data;
        this.isLastPage = isLastPage;
    }

    public static ApiResponse ok() {
        return new ApiResponse(200, "", new HashMap<>(), false);
    }

    public static ApiResponse ok(Object data) {
        if (data instanceof ResponseListEntity) {
            return new ApiResponse(200, "", ((ResponseListEntity<?>) data).getList(), !((ResponseListEntity) data).isHasNext());
        }
        return new ApiResponse(200, "", data, false);
    }

    public static ApiResponse error(int code, String errorMsg) {
        return new ApiResponse(code, errorMsg, new HashMap<>(), false);
    }
}
